/**
 * 商品分享功能相关的JavaScript代码
 */

// 初始化分享功能
$(function() {
    // 分享按钮点击事件
    $('#shareBtn').click(function() {
        $('#shareModal').modal('show');
    });
});

/**
 * 记录分享行为
 * @param goodsId 商品ID
 * @param type 分享类型: wechat, weibo, qq, link
 */
function recordShare(goodsId, type) {
    $.ajax({
        url: 'share_record',
        type: 'POST',
        data: {
            goodsid: goodsId,
            type: type
        }
    });
}

/**
 * 分享到微信，显示二维码
 * @param goodsId 商品ID
 */
function shareToWechat(goodsId) {
    recordShare(goodsId, 'wechat');
    var qrcodeUrl = 'qrcode?id=' + goodsId;
    layer.open({
        type: 1,
        title: '微信分享',
        content: '<div class="text-center"><p>打开微信扫一扫，分享给好友</p><img src="' + qrcodeUrl + '" width="200" height="200" /></div>',
        area: ['300px', '350px']
    });
}

/**
 * 分享到微博
 * @param goodsId 商品ID
 * @param title 分享标题
 */
function shareToWeibo(goodsId, title) {
    recordShare(goodsId, 'weibo');
    var url = window.location.href;
    var shareUrl = 'http://service.weibo.com/share/share.php?url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title);
    window.open(shareUrl, '_blank');
}

/**
 * 分享到QQ
 * @param goodsId 商品ID
 * @param title 分享标题
 */
function shareToQQ(goodsId, title) {
    recordShare(goodsId, 'qq');
    var url = window.location.href;
    var shareUrl = 'http://connect.qq.com/widget/shareqq/index.html?url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title);
    window.open(shareUrl, '_blank');
}

/**
 * 复制分享链接
 * @param goodsId 商品ID
 */
function copyShareLink(goodsId) {
    recordShare(goodsId, 'link');
    var url = window.location.href;
    
    // 创建临时输入框
    var tempInput = document.createElement('input');
    tempInput.value = url;
    document.body.appendChild(tempInput);
    tempInput.select();
    document.execCommand('copy');
    document.body.removeChild(tempInput);
    
    layer.msg('链接已复制到剪贴板');
} 